import json
import datetime

from flask import Blueprint
from flask import request
from flask import Response

from sqlalchemy import Column, String, Integer
from flask_sqlalchemy import SQLAlchemy

from app.comm.db import db
from app.system import auth
from app.comm.db import jsonformater
from app.comm.pub import RestEntity
from app.comm.pub import class2dict

userservice = Blueprint('user', __name__, )


class DataResource(db.Model):
    __tablename__ = 'data_resource' 
    res_id= Column(Integer, primary_key=True)
    res_name= Column(String(80), unique=True, nullable=False)
    author= Column(String(80), unique=True, nullable=False)
    max_type= Column(String(80), unique=True, nullable=False)
    mid_type= Column(String(80), unique=True, nullable=False)
    min_type= Column(String(80), unique=True, nullable=False)
    order_num= Column(String(80), unique=True, nullable=False)
    icon= Column(String(80), unique=True, nullable=False)
    url= Column(String(80), unique=True, nullable=False)
    memo= Column(String(80), unique=True, nullable=False)
    visible= Column(String(80), unique=True, nullable=False)
    create_by= Column(String(80), unique=True, nullable=False)
    create_time= Column(String(80), unique=True, nullable=False)
    update_by= Column(String(80), unique=True, nullable=False)
    update_time= Column(String(80), unique=True, nullable=False)
    remark= Column(String(80), unique=True, nullable=False)

    def as_dict(self):
        return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}

    def __repr__(self):
        return '<User %r>' % self.res_id


@userservice.route("/rest/res/data/list1", methods=['POST', 'GET'])
def userLogin():
    print(request.data)
    logindata = json.loads(request.data)
    tmpuser = User.query.filter_by(user_name='管理员').first().as_dict()
    # dict1["email"]=tmpuser.email
    print(tmpuser)
    # 获取token/
    token = auth.encode_auth_token(logindata["username"], 10).decode()
    # jsonify
    # jsonify(RestEntity(0,0,tmpuser,"登陆成功！",token))


    result = RestEntity(0, 0, tmpuser, "登陆成功！", token)
    # print(json.dumps(class2dict(result)))
    return json.dumps(class2dict(result))


# return json.dumps(dict1)
@userservice.route("/rest/res/data/list", methods=['POST', 'GET'])
def userList():
    appid = request.get_data()

    # dict1 = json.loads(a)
    tables = db.session.execute(
        "select res_id,res_name,author,max_type,mid_type,min_type,order_num,icon,url,memo,visible,create_by,create_time,update_by,update_time,remark  from data_resource").fetchall()
    results = cursor.fetchall()
        for row in results:
            id = row[0]
            stream_name = row[1]
            download_url = row[2]
            size = row[3]
            status = row[4]
            createAt = row[5]	
            print(id,stream_name,download_url,size,status,createAt)
    
    datas = [dict(r) for r in tables]
    result = RestEntity(0, 0, datas, "获取数据！")
    return json.dumps(class2dict(result), default=jsonformater)

    # return json.dumps(dict1)